﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace VisionRoc_Measure
{
    public partial class FormDeepMatchTool : Form
    {
        GenericModelMatch m_Match;
        HObject m_Image;

        public FormDeepMatchTool(GenericModelMatch _Match, HObject _Image)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DoubleBuffered = true;//避免时间显示控件闪烁
            InitializeComponent();
            m_Match = _Match;
            this.m_Image = _Image;
        }

        private void FormDeepMatchTool_Load(object sender, EventArgs e)
        {
            
            HOperatorSet.SetDraw(this.hWindowControl1.HalconWindow, "margin");
            HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
            HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
            HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
            HOperatorSet.CountObj(m_Image, out HTuple tuple1);
            if (m_Image == null || tuple1.I == 0)
            {
                tool_ResultInfo.Text = "图像为空！";
            }
            else
            {
                HTuple Width = new HTuple(), Height = new HTuple();
                HOperatorSet.GetImageSize(m_Image, out Width, out Height);
                HOperatorSet.SetSystem("width", Width);
                HOperatorSet.SetSystem("height", Height);
                MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                m_Image.DispObj(hWindowControl1.HalconWindow);
                tool_ResultInfo.Text = "图像加载成功。";
            }
            if (m_Match != null)
            {
                if (File.Exists(m_Match.m_Path))
                {
                    m_Match.ReadMatchParameter();
                    if (m_Match.SearchRegionRow1 == 0 && m_Match.SearchRegionCol1 == 0)
                    {
                        tool_ResultInfo.Text += "全图搜索模式中。";
                    }
                    else
                    {
                        tool_ResultInfo.Text += "区域搜索模式中、";
                    }
                }
                else
                {
                    tool_ResultInfo.Text += "模块参数文件不存在。请重新创建保存。";
                }

                this.propertyGrid1.SelectedObject = m_Match;
                this.propertyGrid1.Refresh();
            }

        }

        /// <summary>
        /// 恢复系统默认
        /// </summary>
        public void SystemDefault()
        {
            HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
            HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
            HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
        }

        /// <summary>
        /// 全图模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_TotalGraph_Click(object sender, EventArgs e)
        {
            m_Match.SearchRegionRow1 = 0;
            m_Match.SearchRegionCol1 = 0;
            m_Match.SearchRegionRow2 = 0;
            m_Match.SearchRegionCol2 = 0;
            if (m_Match.SearchRegionRow1 == 0 && m_Match.SearchRegionCol1 == 0)
            {
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                hWindowControl1.HalconWindow.DispObj(m_Image);
                tool_ResultInfo.Text = "设置全图搜索模式成功！";
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "设置全图搜索模式成功。", "window", 30, 30, 20, "green");

            }
        }

        /// <summary>
        /// 区域模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Region_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("请先输入图像");
                    return;
                }
                groupBox2.Enabled = false;
                btn_TotalGraph.Enabled = false;
                btn_CreateModel.Enabled = false;
                btn_FindModel.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                btn_Region.Enabled = false;
                SystemDefault();
                hWindowControl1.Focus();
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                hWindowControl1.HalconWindow.DispObj(m_Image);
                //绘制和生成ROI
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "左键绘制，右键结束", "window", 30, 30, 20, "green");
                HTuple SearchRegionRow1 = new HTuple(), SearchRegionCol1 = new HTuple(), SearchRegionRow2 = new HTuple(), SearchRegionCol2 = new HTuple();
                SearchRegionRow1.Dispose(); SearchRegionCol1.Dispose(); SearchRegionRow2.Dispose(); SearchRegionCol2.Dispose();
                HOperatorSet.DrawRectangle1(hWindowControl1.HalconWindow, out SearchRegionRow1, out SearchRegionCol1, out SearchRegionRow2, out SearchRegionCol2);

                if (SearchRegionRow1.ToString().Length > 0)
                {
                    m_Match.SearchRegionRow1 = Convert.ToDouble(SearchRegionRow1.ToString());
                    m_Match.SearchRegionCol1 = Convert.ToDouble(SearchRegionCol1.ToString());
                    m_Match.SearchRegionRow2 = Convert.ToDouble(SearchRegionRow2.ToString());
                    m_Match.SearchRegionCol2 = Convert.ToDouble(SearchRegionCol2.ToString());
                    HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    hWindowControl1.HalconWindow.DispObj(m_Image);
                    MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制成功", "window", 30, 30, 20, "green");
                    HObject Region = null;
                    HOperatorSet.GenEmptyObj(out Region);
                    HOperatorSet.GenRectangle1(out Region, SearchRegionRow1, SearchRegionCol1, SearchRegionRow2, SearchRegionCol2);
                    MyHOperatorSet.DispContour(hWindowControl1.HalconWindow, Model.margin, "green", Region);
                    tool_ResultInfo.Text = "绘制区域成功！当前为区域搜索模式。";
                    groupBox2.Enabled = true;
                    btn_TotalGraph.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_Region.Enabled = true;
                }
                else
                {
                    groupBox2.Enabled = true;
                    btn_TotalGraph.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_Region.Enabled = true;
                    tool_ResultInfo.Text = "绘制区域失败！";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 绘制圆模版区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrawCircle_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("请先输入图像");
                    return;
                }
                SystemDefault();
                hWindowControl1.Focus();
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                hWindowControl1.HalconWindow.DispObj(m_Image);
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制圆形模版区域：左键绘制，右键结束", "window", 30, 30, 15, "green");
                HTuple Row = new HTuple(), Col = new HTuple(), Radius = new HTuple();
                Row.Dispose(); Col.Dispose(); Radius.Dispose();
                groupBox1.Enabled = false;
                btn_CreateModel.Enabled = false;
                btn_FindModel.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                btn_DrawCircle.Enabled = false;
                btn_DrawRectangle1.Enabled = false;
                btn_DrawRectangle2.Enabled = false;
                btn_ClearModelRegion.Enabled = false;
                HOperatorSet.DrawCircle(hWindowControl1.HalconWindow, out Row, out Col, out Radius);
                if (Row.D > 0)
                {
                    HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    hWindowControl1.HalconWindow.DispObj(m_Image);
                    m_Match.ModelRegion.Dispose();
                    HOperatorSet.GenCircle(out m_Match.ModelRegion, Row, Col, Radius);
                    MyHOperatorSet.DispContour(hWindowControl1.HalconWindow, Model.margin, "green", m_Match.ModelRegion);
                    MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制圆形模版区域成功。", "window", 30, 30, 15, "green");
                    tool_ResultInfo.Text = "绘制圆形模版创建区域成功！";
                    groupBox1.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_DrawCircle.Enabled = true;
                    btn_DrawRectangle1.Enabled = true;
                    btn_DrawRectangle2.Enabled = true;
                    btn_ClearModelRegion.Enabled = true;
                }
                else
                {
                    groupBox1.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_DrawCircle.Enabled = true;
                    btn_DrawRectangle1.Enabled = true;
                    btn_DrawRectangle2.Enabled = true;
                    btn_ClearModelRegion.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 绘制矩形1模版区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrawRectangle1_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("请先输入图像");
                    return;
                }
                SystemDefault();
                hWindowControl1.Focus();
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                hWindowControl1.HalconWindow.DispObj(m_Image);
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制矩形一模版区域：左键绘制，右键结束", "window", 30, 30, 15, "green");
                HTuple Row1 = new HTuple(), Col1 = new HTuple(), Row2 = new HTuple(), Col2 = new HTuple();
                groupBox1.Enabled = false;
                btn_CreateModel.Enabled = false;
                btn_FindModel.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                btn_DrawCircle.Enabled = false;
                btn_DrawRectangle1.Enabled = false;
                btn_DrawRectangle2.Enabled = false;
                btn_ClearModelRegion.Enabled = false;
                Row1.Dispose(); Col1.Dispose(); Row2.Dispose(); Col2.Dispose();
                HOperatorSet.DrawRectangle1(hWindowControl1.HalconWindow, out Row1, out Col1, out Row2, out Col2);
                if (Row2.D > 0)
                {
                    HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    hWindowControl1.HalconWindow.DispObj(m_Image);
                    m_Match.ModelRegion.Dispose();
                    HOperatorSet.GenRectangle1(out m_Match.ModelRegion, Row1, Col1, Row2, Col2);
                    MyHOperatorSet.DispContour(hWindowControl1.HalconWindow, Model.margin, "green", m_Match.ModelRegion);
                    MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制矩形一模版区域成功。", "window", 30, 30, 15, "green");
                    tool_ResultInfo.Text = "绘制矩形一模版区域成功！";
                    groupBox1.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_DrawCircle.Enabled = true;
                    btn_DrawRectangle1.Enabled = true;
                    btn_DrawRectangle2.Enabled = true;
                    btn_ClearModelRegion.Enabled = true;
                }
                else
                {
                    groupBox1.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_DrawCircle.Enabled = true;
                    btn_DrawRectangle1.Enabled = true;
                    btn_DrawRectangle2.Enabled = true;
                    btn_ClearModelRegion.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 绘制矩形2模版区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrawRectangle2_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("请先输入图像");
                    return;
                }
                SystemDefault();
                hWindowControl1.Focus();
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                hWindowControl1.HalconWindow.DispObj(m_Image);
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制矩形二模版区域：左键绘制，右键结束", "window", 30, 30, 15, "green");
                HTuple Row = new HTuple(), Col = new HTuple(), Phi = new HTuple(), Length1 = new HTuple(), Length2 = new HTuple();
                groupBox1.Enabled = false;
                btn_CreateModel.Enabled = false;
                btn_FindModel.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                btn_DrawCircle.Enabled = false;
                btn_DrawRectangle1.Enabled = false;
                btn_DrawRectangle2.Enabled = false;
                btn_ClearModelRegion.Enabled = false;
                Row.Dispose(); Col.Dispose(); Phi.Dispose(); Length1.Dispose(); Length2.Dispose();
                HOperatorSet.DrawRectangle2(hWindowControl1.HalconWindow, out Row, out Col, out Phi, out Length1, out Length2);
                if (Row.D > 0)
                {
                    HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    hWindowControl1.HalconWindow.DispObj(m_Image);
                    HOperatorSet.GenRectangle2(out m_Match.ModelRegion, Row, Col, Phi, Length1, Length2);
                    MyHOperatorSet.DispContour(hWindowControl1.HalconWindow, Model.margin, "green", m_Match.ModelRegion);
                    MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制矩形二模版区域成功。", "window", 30, 30, 15, "green");
                    tool_ResultInfo.Text = "绘制矩形二模版区域成功！";
                    groupBox1.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_DrawCircle.Enabled = true;
                    btn_DrawRectangle1.Enabled = true;
                    btn_DrawRectangle2.Enabled = true;
                    btn_ClearModelRegion.Enabled = true;
                }
                else
                {
                    groupBox1.Enabled = true;
                    btn_CreateModel.Enabled = true;
                    btn_FindModel.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                    btn_DrawCircle.Enabled = true;
                    btn_DrawRectangle1.Enabled = true;
                    btn_DrawRectangle2.Enabled = true;
                    btn_ClearModelRegion.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 绘制任意区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrawRegion_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("请先输入图像");
                    return;
                }
                SystemDefault();
                hWindowControl1.Focus();
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                hWindowControl1.HalconWindow.DispObj(m_Image);
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制任意区域：左键绘制，右键结束", "window", 30, 30, 15, "green");
                groupBox1.Enabled = false;
                btn_CreateModel.Enabled = false;
                btn_FindModel.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                btn_DrawCircle.Enabled = false;
                btn_DrawRectangle1.Enabled = false;
                btn_DrawRectangle2.Enabled = false;
                btn_ClearModelRegion.Enabled = false;
                HOperatorSet.DrawRegion(out m_Match.ModelRegion, hWindowControl1.HalconWindow);
                HOperatorSet.CountObj(m_Match.ModelRegion, out HTuple tuple11);
                if (tuple11.I != 0)
                {
                    MyHOperatorSet.DispContour(hWindowControl1.HalconWindow, Model.margin, "green", m_Match.ModelRegion);
                    tool_ResultInfo.Text = "任意区域绘制成功！";
                }
                else
                {
                    tool_ResultInfo.Text = "任意区域绘制失败！";
                }
                groupBox1.Enabled = true;
                btn_CreateModel.Enabled = true;
                btn_FindModel.Enabled = true;
                btn_Refresh.Enabled = true;
                btn_SaveParameter.Enabled = true;
                btn_DrawCircle.Enabled = true;
                btn_DrawRectangle1.Enabled = true;
                btn_DrawRectangle2.Enabled = true;
                btn_ClearModelRegion.Enabled = true;
            }
            catch
            {

            }
        }

        /// <summary>
        /// 清除模版区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ClearModelRegion_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            HOperatorSet.GenEmptyObj(out m_Match.ModelRegion);
            HOperatorSet.CountObj(m_Match.ModelRegion, out HTuple tuple1);
            if (tuple1.I == 0)
            {
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "模板创建区域已清空。", "window", 30, 30, 15, "green");
                tool_ResultInfo.Text = "模板创建区域已清空";
            }
            else
            {
                tool_ResultInfo.Text = "模板创建区域清空失败！";
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "模板创建区域清空失败！", "window", 30, 30, 15, "red");
            }
        }

        /// <summary>
        /// 模版学习
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_CreateModel_Click(object sender, EventArgs e)
        {
            if (m_Image == null)// (tuple1.I == 0)
            {
                tool_ResultInfo.Text = "图像为空！";
                MessageBox.Show("请先输入图像");
                return;
            }
            HOperatorSet.CountObj(m_Match.ModelRegion, out HTuple tuple11);
            if (tuple11.I == 0)
            {
                tool_ResultInfo.Text = "模拟区域为空！";
                MessageBox.Show("请先创建模版区域");
                return;
            }
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            m_Match.Create_Generic_Shape_Model(m_Image, hWindowControl1.HalconWindow, out HTuple ModelID);
            sw.Stop();
            if (m_Match.ModelID.Length > 0)
            {
                if (m_Match.WriteShapelModel())
                {
                    tool_ResultInfo.Text = "模板创建成功！已保存。用时" + sw.ElapsedMilliseconds + "ms";
                }
            }
            else
            {
                tool_ResultInfo.Text = "创建模板失败！！";
            }

        }

        /// <summary>
        /// 查找模版
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FindModel_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                Stopwatch sw = new Stopwatch();
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                sw.Start();
                m_Match.Find_Generic_Shape_Model(m_Image, hWindowControl1.HalconWindow, out HTuple HomMat2D);
                sw.Stop();
                double X = m_Match.Score.D;
                if (X > 0)
                {
                    this.tool_ResultInfo.Text = $"查找结果:{m_Match.Score.Length}个 用时：{sw.ElapsedMilliseconds}ms  第一分数:{m_Match.Score[0].D.ToString("F3")} 第一匹配点:X:{m_Match.Col[0].D.ToString("F3")} Y:{m_Match.Row[0].D.ToString("F3")} Angle:{MyHOperatorSet.RadToDeg(m_Match.Angle[0].D).ToString("F3")}";
                }
                else
                {
                    this.tool_ResultInfo.Text = "未找到模版";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        /// <summary>
        /// 基准点设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("是否设置当前坐标为基准点", "基准设置询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                Stopwatch sw = new Stopwatch();
                sw.Start();
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                if (m_Match.DatumPointSet(m_Image, hWindowControl1.HalconWindow))
                {
                    this.tool_ResultInfo.Text = $"基准点设置成功:Row:{m_Match.CreateRow},Col：{m_Match.CreateCol}Angle:{m_Match.CreateAngle}。";
                }
                else
                {
                    this.tool_ResultInfo.Text = "基准设置失败";
                }
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                if (m_Match.SearchRegionRow1 == 0 && m_Match.SearchRegionCol1 == 0)
                {
                    this.tool_ResultInfo.Text = "全图搜索模式中";
                }
                else
                {
                    this.tool_ResultInfo.Text = "区域搜索模式中";
                }
                this.propertyGrid1.SelectedObject = m_Match;
                this.propertyGrid1.Refresh();
                tool_ResultInfo.Text = "刷新成功";
            }
            catch (Exception ex)
            {
                tool_ResultInfo.Text = "刷新失败！";
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 保存模版
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SaveParameter_Click(object sender, EventArgs e)
        {
            try
            {
                m_Match.WriteMatchParameter();
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 缩放图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            MyHOperatorSet.ZoomImage(hWindowControl1, m_Image, e);
        }

        /// <summary>
        /// 适应图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 适应图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);

        }

        /// <summary>
        /// 保存原图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存原图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveImage(m_Image);
        }

        /// <summary>
        /// 保存窗口图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存窗口图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveWindowDump(hWindowControl1.HalconWindow);
        }


    }
}
